package com.rongcheng.fluxtest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.rongcheng.fluxtest.net.requestCallback.LoginRequestCallback;
import com.rongcheng.fluxtest.stores.LoginStore;
import com.rongcheng.fluxtest.stores.Store;
import com.rongcheng.frc.androidlib.net.YXNetWorkManager;
import com.squareup.otto.Subscribe;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private EditText mEdtLoginName, mEdtLoginPassword;
    private Button mBtnLogin;
    private LoginStore loginStore = new LoginStore();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }


    private void initView() {
        mEdtLoginName = (EditText) findViewById(R.id.edt_login_name);
        mEdtLoginPassword = (EditText) findViewById(R.id.edt_login_password);
        mBtnLogin = (Button) findViewById(R.id.btn_login);
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login(mEdtLoginName.getText().toString(), mEdtLoginPassword.getText().toString());
            }


        });
    }

    private void login(String loginName, String loginPassword) {
        Map<String, String> params = new HashMap<>();
        params.put("loginName", loginName);
        params.put("password", loginPassword);
        YXNetWorkManager.getInstance().invoke(MainActivity.this, "login", params, new LoginRequestCallback());
    }

    @Subscribe
    public void onStoreChange(Store.StoreChangeEvent event){
        render(loginStore);
    }

    private void render(LoginStore loginStore) {
        loginStore.getLoginMessage();
    }
}
